﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RecruitStudents.Utility;
using System.Data;
using Hxj.Data;
using RecruitStudents.Utility.Model;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Const;
using RecruitStudents.Utility.Json;

namespace RecruitStudents.Client.BLL.Impl
{
    public class BLLSecretary
    {
        DAL.DALSecretary dal = new DAL.DALSecretary();

        /// <summary>
        /// 秘书根据分组获取抽签信息
        /// </summary>
        /// <param name="strGroupNo">分组号</param>
        /// <param name="strExamRoomID">考场ID</param>
        /// <returns></returns>
        public DataTable GetScoreQuery_Sec(string strGroupNO, string strExamRoomID)
        {
            WhereClip wcp = new WhereClip();
            wcp = wcp.And(TB_Exam_Signed._.GroupNO == strGroupNO && TB_Pad_Queue._.ExamRoomNO == strExamRoomID);

            return DbHelper.GetDatabase().From<TB_Pad_Queue>().LeftJoin<TB_Exam_Signed>(TB_Pad_Queue._.SignedID == TB_Exam_Signed._.SignedID)
                     .LeftJoin<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(TB_Exam_Signed._.GroupNO == TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.GroupNo)
                     .Select(TB_Pad_Queue._.All, TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.GroupName).Where(wcp)
                     .OrderBy(new OrderByClip(TB_Pad_Queue._.LotNO, OrderByOperater.ASC)).ToDataTable();
        }

        /// <summary>
        /// 秘书根据分组获取正在考试信息
        /// </summary>
        /// <param name="strGroupNo">分组号</param>
        /// <returns></returns>
        public DataTable GetScoreQuery_Sec_KS(string strGroupNo, string strExamRoomID)
        {
            WhereClip wcp = new WhereClip();
            if (string.IsNullOrEmpty(strGroupNo))
            {
                wcp = wcp.And(TB_Pad_Examing._.ExamRoomNO == strExamRoomID);
            }
            else
            {
                wcp = wcp.And(TB_Pad_Examing._.GroupNo == strGroupNo && TB_Pad_Examing._.ExamRoomNO == strExamRoomID);
            }
            return DbHelper.GetDatabase().From<TB_Pad_Examing>()
                  .LeftJoin<TB_Sys_ExamPlaceSpecialityKindsGroupMapping>(TB_Pad_Examing._.GroupNo == TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.GroupNo)
                  .Select(TB_Pad_Examing._.All, TB_Sys_ExamPlaceSpecialityKindsGroupMapping._.GroupName).Where(wcp)
                  .OrderBy(new OrderByClip(TB_Pad_Examing._.LotNO, OrderByOperater.ASC))
                  .ToDataTable();
        }

        public DataTable GetTeacherNoDF(string strGroupNo, string strExamRoomID)
        {
            string strSql = @"select distinct  a.ExaminerName,a.LoginName,d.ScoreAll from TB_Exam_Examiner as a 
			     left join TB_Exam_ExamRoomExaminerMapping as b on b.ExaminerID=a.ExaminerID 
                 left join TB_Pad_Examing as c on c.ExamRoomNO=b.ExamRoomNO and c.groupNO='{0}' and c.ExamRoomNO='{1}' 
                 left join TB_Pad_TeacherScoreList as d on a.LoginName=d.LoginName            
              where a.IsActive='1' and c.groupNO='{0}' and c.ExamRoomNO='{1}' order by d.ScoreAll asc ";
            strSql = string.Format(strSql, strGroupNo, strExamRoomID);
            return DbHelper.GetDatabase().FromSql(strSql).ToDataTable();
        }

        public JsonMessage SaveSecExaming(string strNum, string strGroupNo, string strExamRoomID, string strExamRoomName)
        {
            JsonMessage message = new JsonMessage();
            using (DataTable dtTeacher = ServiceHelper.CreateCommService().GetTeacherList(strExamRoomID))
            {
                TB_Sys_SystemInfo model = ServiceHelper.CreateCommService().GetSystemInfoModel(Utility.Const.ConstSystem.SystemYear);
                string strYear = DateTime.Now.Year.ToString();
                if (model != null)
                {
                    strYear = model.SystemInfoName;
                }
                string strMessage = dal.SaveSecExaming(strNum, strGroupNo, strExamRoomID, strExamRoomName, dtTeacher, strYear);
                if (strMessage.Length == 0)
                {
                    message.State = "true";
                    message.Msg = "";
                }
                else
                {
                    message.State = "false";
                    message.Msg = strMessage;
                }
                return message;
            }
        }



        /// <summary>
        /// 通过考场和组来计算平均分
        /// </summary>
        /// <param name="strAGT_ID"></param>
        /// <param name="strExamRoomID"></param>
        /// <returns></returns>
        public JsonMessage SaveCalculationAvgPoints(string strAGT_ID, string strExamRoomID)
        {
            JsonMessage message = new JsonMessage();
            try
            {
                List<TB_Pad_StudentScoreList> listStudentScoreUpd = new List<TB_Pad_StudentScoreList>();
                List<TB_Pad_StudentScoreList> listStudentScore = dal.GetStudentScoreList(strAGT_ID, strExamRoomID); 
                using (DataTable dtTeacher = ServiceHelper.CreateCommService().GetTeacherList(strExamRoomID))
                {
                    for (int i = 0; i < listStudentScore.Count; i++)
                    {
                        TB_Pad_StudentScoreList modelStudentScore = listStudentScore[i];
                        modelStudentScore.Attach();
                        string strExamLevel = modelStudentScore.ExamLevel.ToSafeString();
                        string strSpecialityKindsNO = modelStudentScore.SpecialityKindsNO;
                        using (DataTable dt = dal.GetCalculateScoreTypeTable(strSpecialityKindsNO, strExamLevel))
                        {
                            if (dt.Rows.Count > 0)
                            {
                                string strCalculateScoreType = dt.Rows[0]["CalculateScoreType"].ToSafeString();
                                if (strCalculateScoreType == ConstAppConfig.CalculateType_SUM)
                                {
                                    modelStudentScore.TatolScore = modelStudentScore.Score1.ToSafeDecimal() + modelStudentScore.Score2.ToSafeDecimal()
                                        + modelStudentScore.Score3.ToSafeDecimal() + modelStudentScore.Score4.ToSafeDecimal()
                                        + modelStudentScore.Score5.ToSafeDecimal() + modelStudentScore.Score6.ToSafeDecimal()
                                        + modelStudentScore.Score7.ToSafeDecimal();
                                }
                                else if (strCalculateScoreType == ConstAppConfig.CalculateType_AVG)
                                {
                                    modelStudentScore.TatolScore = GetScoreAVG(modelStudentScore);
                                }
                                else if (strCalculateScoreType == ConstAppConfig.CalculateType_AVG_OUT)
                                {
                                    modelStudentScore = BuildStudentScoreList_Update(modelStudentScore);
                                }
                                else if (strCalculateScoreType == ConstAppConfig.CalculateType_AVG_WEIGHT)
                                {

                                }
                            }
                        }



                        //if (strSpecialityKindsNO == ConstSpecialityKinds.BY_Value)//表演
                        //{
                        //    #region 表演
                        //    if (strExamLevel == ConstExamLevel.CS_Value)//初试
                        //    {
                        //        modelStudentScore.TatolScore = modelStudentScore.Score1.ToSafeDecimal() + modelStudentScore.Score2.ToSafeDecimal()
                        //            + modelStudentScore.Score3.ToSafeDecimal() + modelStudentScore.Score4.ToSafeDecimal()
                        //            + modelStudentScore.Score5.ToSafeDecimal() + modelStudentScore.Score6.ToSafeDecimal()
                        //            + modelStudentScore.Score7.ToSafeDecimal();
                        //    }
                        //    else if (strExamLevel == ConstExamLevel.FS_Value)//复试
                        //    {
                        //        modelStudentScore.TatolScore = modelStudentScore.Score1.ToSafeDecimal() + modelStudentScore.Score2.ToSafeDecimal()
                        //            + modelStudentScore.Score3.ToSafeDecimal() + modelStudentScore.Score4.ToSafeDecimal()
                        //            + modelStudentScore.Score5.ToSafeDecimal() + modelStudentScore.Score6.ToSafeDecimal()
                        //            + modelStudentScore.Score7.ToSafeDecimal();
                        //    }
                        //    else if (strExamLevel == ConstExamLevel.SS_Value)//三试
                        //    {
                        //        modelStudentScore = BuildStudentScoreList_Update(modelStudentScore);
                        //    }
                        //    #endregion
                        //}
                        //else if (strSpecialityKindsNO == ConstSpecialityKinds.GQ_Value)//钢琴
                        //{
                        //    #region 钢琴
                        //    if (strExamLevel == ConstExamLevel.CS_Value)//初试
                        //    {
                        //        modelStudentScore.TatolScore = modelStudentScore.Score1.ToSafeDecimal() + modelStudentScore.Score2.ToSafeDecimal()
                        //            + modelStudentScore.Score3.ToSafeDecimal() + modelStudentScore.Score4.ToSafeDecimal()
                        //            + modelStudentScore.Score5.ToSafeDecimal() + modelStudentScore.Score6.ToSafeDecimal()
                        //            + modelStudentScore.Score7.ToSafeDecimal();
                        //    }
                        //    else if (strExamLevel == ConstExamLevel.FS_Value)//复试
                        //    {
                        //        modelStudentScore = BuildStudentScoreList_Update(modelStudentScore);
                        //    }
                        //    #endregion
                        //}
                        //else if (strSpecialityKindsNO == ConstSpecialityKinds.SY_Value)//声乐
                        //{
                        //    #region 声乐
                        //    if (strExamLevel == ConstExamLevel.CS_Value)//初试
                        //    {
                        //        modelStudentScore.TatolScore = modelStudentScore.Score1.ToSafeDecimal() + modelStudentScore.Score2.ToSafeDecimal()
                        //            + modelStudentScore.Score3.ToSafeDecimal() + modelStudentScore.Score4.ToSafeDecimal()
                        //            + modelStudentScore.Score5.ToSafeDecimal() + modelStudentScore.Score6.ToSafeDecimal()
                        //            + modelStudentScore.Score7.ToSafeDecimal();
                        //    }
                        //    else if (strExamLevel == ConstExamLevel.FS_Value)//复试
                        //    {
                        //        modelStudentScore = BuildStudentScoreList_Update(modelStudentScore);
                        //    }
                        //    #endregion
                        //}
                        //else if (strSpecialityKindsNO == ConstSpecialityKinds.GXY_Value)//管弦乐
                        //{
                        //    #region 管弦乐
                        //    if (strExamLevel == ConstExamLevel.CS_Value)//初试
                        //    {
                        //        modelStudentScore.TatolScore = modelStudentScore.Score1.ToSafeDecimal() + modelStudentScore.Score2.ToSafeDecimal()
                        //            + modelStudentScore.Score3.ToSafeDecimal() + modelStudentScore.Score4.ToSafeDecimal()
                        //            + modelStudentScore.Score5.ToSafeDecimal() + modelStudentScore.Score6.ToSafeDecimal()
                        //            + modelStudentScore.Score7.ToSafeDecimal();
                        //    }
                        //    else if (strExamLevel == ConstExamLevel.FS_Value)//复试
                        //    {
                        //        decimal decGYPWFS = 0;//管乐评委分数
                        //        decimal decXYPWFS = 0;//弦乐评委分数
                        //        foreach (DataRow drTeacher in dtTeacher.Rows)
                        //        {
                        //            int techxh = drTeacher["XH"].ToSafeInt();//组打分老师排序号
                        //            int TeacherType = drTeacher["Type"].ToSafeInt();//1：管乐评委 2：弦乐评委 ）
                        //            if (TeacherType == 1)//1：管乐评委
                        //            {
                        //                #region 管乐评委
                        //                if (techxh == 1)
                        //                {
                        //                    decGYPWFS = decGYPWFS + modelStudentScore.Score1.ToSafeDecimal();
                        //                }
                        //                else if (techxh == 2)
                        //                {
                        //                    decGYPWFS = decGYPWFS + modelStudentScore.Score2.ToSafeDecimal();
                        //                }
                        //                else if (techxh == 3)
                        //                {
                        //                    decGYPWFS = decGYPWFS + modelStudentScore.Score3.ToSafeDecimal();
                        //                }
                        //                else if (techxh == 4)
                        //                {
                        //                    decGYPWFS = decGYPWFS + modelStudentScore.Score4.ToSafeDecimal();
                        //                }
                        //                else if (techxh == 5)
                        //                {
                        //                    decGYPWFS = decGYPWFS + modelStudentScore.Score5.ToSafeDecimal();
                        //                }
                        //                else if (techxh == 6)
                        //                {
                        //                    decGYPWFS = decGYPWFS + modelStudentScore.Score6.ToSafeDecimal();
                        //                }
                        //                else if (techxh == 7)
                        //                {
                        //                    decGYPWFS = decGYPWFS + modelStudentScore.Score7.ToSafeDecimal();
                        //                }
                        //                #endregion
                        //            }
                        //            else if (TeacherType == 2)//   2：弦乐评委 ）
                        //            {
                        //                #region 弦乐评委
                        //                if (techxh == 1)
                        //                {
                        //                    decXYPWFS = decXYPWFS + modelStudentScore.Score1.ToSafeDecimal();
                        //                }
                        //                else if (techxh == 2)
                        //                {
                        //                    decXYPWFS = decXYPWFS + modelStudentScore.Score2.ToSafeDecimal();
                        //                }
                        //                else if (techxh == 3)
                        //                {
                        //                    decXYPWFS = decXYPWFS + modelStudentScore.Score3.ToSafeDecimal();
                        //                }
                        //                else if (techxh == 4)
                        //                {
                        //                    decXYPWFS = decXYPWFS + modelStudentScore.Score4.ToSafeDecimal();
                        //                }
                        //                else if (techxh == 5)
                        //                {
                        //                    decXYPWFS = decXYPWFS + modelStudentScore.Score5.ToSafeDecimal();
                        //                }
                        //                else if (techxh == 6)
                        //                {
                        //                    decXYPWFS = decXYPWFS + modelStudentScore.Score6.ToSafeDecimal();
                        //                }
                        //                else if (techxh == 7)
                        //                {
                        //                    decXYPWFS = decXYPWFS + modelStudentScore.Score7.ToSafeDecimal();
                        //                }
                        //                #endregion
                        //            }
                        //        }
                        //        decimal dec07 = Convert.ToDecimal(0.7);
                        //        decimal dec03 = Convert.ToDecimal(0.3);
                        //        decimal decAVG = 0;
                        //        using (DataTable dt = dal.GetSignUpStudentsSpeciality(modelStudentScore.AllowExamID, strExamRoomID))
                        //        {
                        //            if (dt.Rows.Count > 0)
                        //            {
                        //                int StudentType = dt.Rows[0]["IsPipeChord"].ToSafeInt();//考生类别1管乐考生2弦乐考生
                        //                if (StudentType == 1)//管乐考生
                        //                {
                        //                    decAVG = Math.Round(((decGYPWFS * dec07) + (decXYPWFS * dec03)) / 3, 2);
                        //                }
                        //                else if (StudentType == 2)
                        //                {
                        //                    decAVG = Math.Round(((decGYPWFS * dec03) + (decXYPWFS * dec07)) / 3, 2);
                        //                }
                        //                modelStudentScore.TatolScore = decAVG;
                        //            }
                        //        }
                        //    }
                        //    #endregion
                        //}
                        //listStudentScoreUpd.Add(modelStudentScore);
                    }
                    //dalPad.SaveExamStudentsScoreList(listStudentScoreUpd, null);
                }
                message.State = "true";
                message.Msg = "";
            }
            catch (Exception ex)
            {
                message.State = "false";
                message.Msg = ex.Message;
            }
            return message;
        }

        #region 算平均分, 去掉最高分最低分, 平均分
        /// <summary>
        /// 算平均分, 去掉最高分最低分, 平均分
        /// </summary>
        /// <param name="obj">学生分数实体</param>
        /// <param name="xh">老师打分序号</param>
        /// <param name="strIdCard">老师登录名</param>
        /// <param name="strDisplayName">老师姓名</param>
        /// <param name="decTeacheDF">老师打分值</param>
        /// <returns></returns>
        private TB_Pad_StudentScoreList BuildStudentScoreList_Update(TB_Pad_StudentScoreList obj)
        {
            TB_Pad_StudentScoreList modelStudent = obj;
            decimal decAvg = GetScoreAVG_Out(obj);
            modelStudent.TatolScore = decAvg;//赋值平均分
            return modelStudent;
        }
        #region 算平均分, 去掉最高分最低分, 平均分
        /// <summary>
        /// 算平均分, 去掉最高分最低分, 平均分
        /// </summary>
        /// <param name="obj">TB_Pad_StudentScoreList实体</param>
        /// <param name="xh">老师打分序号</param>
        /// <param name="decTeacheDF">老师打分值</param>
        /// <returns></returns>
        private static decimal GetScoreAVG_Out(TB_Pad_StudentScoreList obj)
        {
            decimal decAvg = 0;
            if (obj != null)
            {
                #region 定义一个泛型list，用来存储考试的个分
                List<decimal> list = new List<decimal>(); //定义一个泛型list，用来存储考试的个分
                if (obj.Teacher1.ToSafeString().Length > 0)//判断老师1是否已打分,如没打分就把老师打分值添加到List
                {
                    decimal decScore1 = obj.Score1.ToSafeDecimal();
                    list.Add(decScore1);
                }
                if (obj.Teacher2.ToSafeString().Length > 0)//判断老师2是否已打分,如没打分, 就把老师打分值添加到List
                {
                    decimal decScore2 = obj.Score2.ToSafeDecimal();
                    list.Add(decScore2);
                }
                if (obj.Teacher3.ToSafeString().Length > 0)//判断老师3是否已打分,如没打分, 就把老师打分值添加到List
                {
                    decimal decScore3 = obj.Score3.ToSafeDecimal();
                    list.Add(decScore3);
                }
                if (obj.Teacher4.ToSafeString().Length > 0)//判断老师4是否已打分,如没打分, 就把老师打分值添加到List
                {
                    decimal decScore4 = obj.Score4.ToSafeDecimal();
                    list.Add(decScore4);
                }
                if (obj.Teacher5.ToSafeString().Length > 0)//判断老师5是否已打分,如没打分, 就把老师打分值添加到List
                {
                    decimal decScore5 = obj.Score5.ToSafeDecimal();
                    list.Add(decScore5);
                }
                if (obj.Teacher6.ToSafeString().Length > 0)//判断老师6是否已打分,如没打分, 就把老师打分值添加到List
                {
                    decimal decScore6 = obj.Score6.ToSafeDecimal();
                    list.Add(decScore6);
                }
                if (obj.Teacher7.ToSafeString().Length > 0)//判断老师7是否已打分,如没打分, 就把老师打分值添加到List
                {
                    decimal decScore7 = obj.Score7.ToSafeDecimal();
                    list.Add(decScore7);
                }
                if (list.Count == 1)
                {
                    decAvg = list[0];
                }
                else if (list.Count == 2)
                {
                    decimal total = 0;
                    foreach (decimal len in list)
                    {
                        total = total + len;
                    }
                    decAvg = Math.Round((total / 2).ToSafeDecimal(), 2);
                }
                else if (list.Count >= 3)
                {
                    list.Sort();
                    list.RemoveAt(0); //删除最小的分数
                    list.RemoveAt(list.Count - 1); //删除最大的分数
                    decimal total = 0;
                    foreach (decimal len in list)
                    {
                        total = total + len;
                    }
                    decAvg = Math.Round((total / (list.Count).ToSafeDecimal()), 2);
                }
                #endregion
            }
            return decAvg;
        }
        #endregion
        #endregion

        #region 算平均分
        /// <summary>
        /// 算平均分
        /// </summary>
        /// <param name="obj">TB_Pad_StudentScoreList实体</param>
        /// <param name="xh">老师打分序号</param>
        /// <param name="decTeacheDF">老师打分值</param>
        /// <returns></returns>
        private static decimal GetScoreAVG(TB_Pad_StudentScoreList obj)
        {
            decimal decAvg = 0;
            if (obj != null)
            {
                #region 定义一个泛型list，用来存储考试的个分
                List<decimal> list = new List<decimal>(); //定义一个泛型list，用来存储考试的个分
                if (obj.Teacher1.ToSafeString().Length > 0)//判断老师1是否已打分,如没打分就把老师打分值添加到List
                {
                    decimal decScore1 = obj.Score1.ToSafeDecimal();
                    list.Add(decScore1);
                }
                if (obj.Teacher2.ToSafeString().Length > 0)//判断老师2是否已打分,如没打分, 就把老师打分值添加到List
                {
                    decimal decScore2 = obj.Score2.ToSafeDecimal();
                    list.Add(decScore2);
                }
                if (obj.Teacher3.ToSafeString().Length > 0)//判断老师3是否已打分,如没打分, 就把老师打分值添加到List
                {
                    decimal decScore3 = obj.Score3.ToSafeDecimal();
                    list.Add(decScore3);
                }
                if (obj.Teacher4.ToSafeString().Length > 0)//判断老师4是否已打分,如没打分, 就把老师打分值添加到List
                {
                    decimal decScore4 = obj.Score4.ToSafeDecimal();
                    list.Add(decScore4);
                }
                if (obj.Teacher5.ToSafeString().Length > 0)//判断老师5是否已打分,如没打分, 就把老师打分值添加到List
                {
                    decimal decScore5 = obj.Score5.ToSafeDecimal();
                    list.Add(decScore5);
                }
                if (obj.Teacher6.ToSafeString().Length > 0)//判断老师6是否已打分,如没打分, 就把老师打分值添加到List
                {
                    decimal decScore6 = obj.Score6.ToSafeDecimal();
                    list.Add(decScore6);
                }
                if (obj.Teacher7.ToSafeString().Length > 0)//判断老师7是否已打分,如没打分, 就把老师打分值添加到List
                {
                    decimal decScore7 = obj.Score7.ToSafeDecimal();
                    list.Add(decScore7);
                }
                decimal total = 0;
                foreach (decimal len in list)
                {
                    total = total + len;
                }
                decAvg = Math.Round((total / (list.Count).ToSafeDecimal()), 2);
                #endregion
            }
            return decAvg;
        }
        #endregion
    }
}
